<!-- 任务详情 -->
<template>
	<view class="page">
		<cu-custom bgColor="bg-white" :isBack="true" :isrefresh="true">
			<block style="color: #777777;font-size: 48rpx;" slot="content">任务详情</block>
		</cu-custom>
		<view class="content">
			<ul>
				<li>
					<text>任务名称：{{name | strFormat}}</text>
				</li>
				<li>
					<text>负责区域：{{regionName | strFormat}}</text>
				</li>
				<li>
					<text>任务内容：{{content | strFormat}}</text>
				</li>
				<li>
					<text>任务类型：{{typeName | strFormat}}</text>
				</li>
				<li>
					<text>起止时间：{{time | strFormat}}</text>
				</li>
			</ul>
		</view>

		<view style="border: 1px double #DCDCDC ;height: 600rpx;overflow-y: auto;margin-top: 40rpx;">
			<scroll-view>
				<view class="xj_record" v-for="(item,index) in taskInfoVos" :key="index">
					<view class="left_p">
						<text class="title">第{{index+1}}次巡检</text>
						<text class="time">开始时间：{{item.startTime}}</text>
						<text class="time">结束时间：{{item.endTime}}</text>
					</view>
					<view class="right_p">
						<!-- <button class="cu-btn bg-system-orange" @tap="ksxj(item)">开始巡检</button> -->
						<button class="cu-btn bg-gradual-blue" @tap="xjzp(item,index)">巡检照片</button>
					</view>
				</view>
			</scroll-view>
		</view>




		<view class="flex btns-group">
			<button class="my-btn-grpup" @click="jb">交班</button>
			<button class="my-btn-grpup" @click="rwth">任务退回</button>
		</view>

		<view class="cu-modal" :class="mdShow">
			<view class="cu-dialog">
				<view class="cu-bar margin-top bg-white">
					<button class="action text-green" @tap="postYh">图片上传</button>
					<button class="action text-blue" @tap="postGJ">隐患上报</button>
				</view>
				<view class="padding-xl">
					<image :src="pimg" style="width: 80%;height: 600rpx;"></image>
				</view>
			</view>
		</view>

	</view>
</template>


<script>
	import * as constants from '@/common/config.js';
	import {
		isDateBetween,
		isEmpty
	} from '@/common/formatDate.js'
	export default {
		onLoad(option) {
			// 封装参数
			this.ucId = option.ucId
			this.xq()
			this.getSpace()
		},

		data() {
			return {
				name: '',
				regionName: '',
				content: '',
				typeName: '',
				time: '',
				ucId: '',
				imgArr: [],
				taskInfoVos: [],
				mdShow: '',
				pimg: '',
				djId: '',
				space: 0
			}
		},
		methods: {
			getSpace() {

				let app = getApp()
				app.globalData.request({
					url: 'wx/taskmanage/getPatrolSpace',
					data: {
						"taskType": "0"
					}
				}).then(function(res) {
					console.info(res.data)
					this.space = res.data.space

				}).catch(function(e) {

				})
			},
			jb() {
				let that = this
				let params = {
					"ucId": this.ucId
				}
				// 交班
				let app = getApp()
				app.globalData.request({
					url: 'wx/taskmanage/taskChange',
					data: {
						...params
					}
				}).then(function(res) {
					if (res.data.code == 0) {
						uni.showToast({
							title: "交班成功",
							icon: 'success'
						})
						that.goBack()
					}

				}).catch(function(e) {
					uni.showToast({
						title: "操作失败",
						icon: 'error'
					})
				})
			},
			rwth() {
				let params = {
					"ucId": this.ucId
				}
				let app = getApp()
				let that = this
				app.globalData.request({
					url: 'wx/taskmanage/taskReturn',
					data: {
						...params
					}
				}).then(function(res) {
					if (res.data.code == 0) {
						uni.showToast({
							title: "任务退回成功",
							icon: 'success'
						})
						that.goBack()
					}

				}).catch(function(e) {
					console.info(e)
					uni.showToast({
						title: "操作失败",
						icon: 'error'
					})
				})

			},
			goBack() {
				uni.navigateBack({
					delta: 2,
					success: () => {
						let page = getCurrentPages().pop()
						if (!page) {
							return;
						} else {
							let option = {
								type: 'refreshPage'
							}
							page.onShow(option)
						}
					}
				})
			},
			ksxj(item) {
				// 是否在时间段内
				let that = this
				let flag = isDateBetween(new Date(), item.startTime, item.endTime)
				if (flag) {
					let params = {
						"ucId": item.ucId
					}
					let app = getApp()
					app.globalData.request({
						url: 'wx/taskmanage/updateTaskInfo',
						data: {
							...params
						}
					}).then(function(res) {
						if (res.data.code == 0) {
							uni.showToast({
								title: "巡检成功",
								icon: 'success'
							})

							// 添加巡检记录
							let myparams = {
								"taskId": that.ucId,
								"taskInfoId": item.ucId
							}
							let app = getApp()
							app.globalData.request({
									url: 'wx/taskmanage/addInsepectionTaskLog',
									data: {
										...myparams
									}
								}).then(function(res) {})
								.catch(function(e) {})

						}

					}).catch(function(e) {
						uni.showToast({
							title: "操作失败",
							icon: 'error'
						})
					})
				} else {
					uni.showToast({
						title: "不在巡检时间段内",
						icon: 'error'
					})
				}
			},
			xjzp(item, index) {
				let flag = isDateBetween(new Date(), item.startTime, item.endTime)
				if (!flag) {
					uni.showToast({
						title: "不在巡检时间段内",
						icon: 'error'
					})
					return;
				}

				if (index > 0) {
					let nowTime = new Date().getTime()
					let endTime = this.taskInfoVos[index - 1].endTime

					//获取上一次执行记录的执行时间
					let lastExecuteTime = this.taskInfoVos[index - 1].executeTime
					if (lastExecuteTime) {
						let executeDate = new Date(lastExecuteTime.replace(/-/, "/"))
						let lastExecuteTimeValue = executeDate.getTime()
						//判断是否超过要求的时间
						if (nowTime - lastExecuteTimeValue < this.space) {
							uni.showToast({
								title: "间隔时间太短，不能巡检！",
								icon: 'error'
							})
							return;
						}
					}
				}


				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'], //从相册选择
					success: (chooseImageRes) => {
						this.imgArr.push(chooseImageRes.tempFilePaths)



						let app = getApp()
						let token = app.globalData.getToken('authorization')
						let that = this
						// 点击上传后循环调用uni.uploadFile ，因微信小程序只支持单文件上传
						that.imgArr.forEach(ele => {
							ele.forEach(item => {
								uni.uploadFile({
									url: constants.API + 'wx/common/upload',
									filePath: item,
									header: {
										"authorization": token
									},
									name: 'file',
									success: (uploadFileRes) => {
										let imgObj = JSON.parse(uploadFileRes.data)

										// 弹窗
										that.mdShow = 'show'
										that.pimg = imgObj.data
										console.info("上传地址:" + imgObj.data)

										that.djId = item.ucId
									}
								});
							})
						})
					}
				});
			},
			postGJ() {
				console.info("******")
				// 跳转隐患工单页面
				uni.navigateTo({
					url: '../yhgd/yhgd?ucId=' + this.djId,
					success() {
						that.mdShow = ''
					}
				})
			},
			postYh() {
				console.info("-----")
				// // submit
				let that = this
				let params = {
					"patrolInfoId": that.djId,
					"imgUrl": that.pimg
				}
				// 生成报警信息
				let app = getApp()
				app.globalData.request({
					url: 'wx/taskmanage/addPatrolImage',
					data: {
						...params
					}
				}).then(function(res) {

					if (res.data.code == 0) {
						that.mdShow = ''
					}
				}).catch(function(e) {})
			},
			xq() {
				let params = {
					"ucId": this.ucId
				}
				let that = this
				// 请求数据
				let app = getApp()
				app.globalData.request({
					url: 'wx/taskmanage/getInEnterpriseTaskVo',
					data: {
						...params
					}
				}).then(function(res) {
					that.name = res.data.data.name
					that.regionName = res.data.data.regionName
					that.content = res.data.data.content
					that.typeName = res.data.data.typeName

					let time = ''
					if (res.data.data.startTime != '') {
						let a1 = res.data.data.startTime.split(" ");
						let t = a1[1].split(":")
						time = t[0] + ":" + t[1]


					}
					if (res.data.data.endTime != '') {
						let a1 = res.data.data.endTime.split(" ");
						let t = a1[1].split(":")
						time = time + " 至 " + t[0] + ":" + t[1]
					}

					that.time = time

					that.taskInfoVos = res.data.data.taskInfoVos

				}).catch(function(e) {

				})
			},
			see_detail(index) {
				console.info("index:" + index)
			},
		}
	}
</script>

<style scoped>
	page {
		width: 100vw;
	}

	.content {
		margin-top: 24rpx;
		background-color: #FFFFFF;
	}

	ul li {
		list-style: none;
		margin: 0;
		padding: 0;
		margin-left: 24rpx;
		position: relative;
	}

	li text {
		font-size: 24rpx;
		color: #777777;
		line-height: 94rpx;
	}

	li::after {
		content: '';
		position: absolute;
		left: 2rpx;
		bottom: 0;
		right: 2rpx;
		height: 1px;
		background-color: #dcdcdc;
	}

	.xj_record {
		height: 193rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin-top: 24rpx;
		background-color: #FFFFFF;
	}

	.left_p {
		margin-left: 24rpx;
		display: flex;
		flex-direction: column;
	}

	.left_p .title {
		margin-top: 14rpx;
		font-size: 24rpx;
		color: #333333;
	}

	.left_p .time {
		font-size: 21rpx;
		color: #777777;
		margin-top: 14rpx;
		padding: 14rpx 0;
	}

	.right_p {
		margin-right: 6rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.my-btn {
		height: 94rpx;
		margin-top: 80px;
		background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
		box-shadow: 0 0 12px 0 rgba(250, 181, 58, 0.50);
		border-radius: 24px;
		ont-family: PingFangSC-Medium;
		color: #FFFFFF;
		letter-spacing: 0;
		text-align: center;
	}

	.my-btn-grpup {
		height: 160rpx;
		line-height: 160rpx;
		vertical-align: middle;
		text-align: center;
	}

	.btns-group {
		width: 100%;
		position: absolute;
		bottom: 0;
		text-align: center;
		display: flex;
		flex-direction: row;
		align-items: flex-end;
	}

	.btns-group button:first-child {
		background-color: #458DF5;
		color: #FFFFFF;
		flex: 1;
		font-size: 36rpx;
		outline: none;
		border: none;
		margin: 0;
		padding: 0;
		border: 1px solid transparent;
		border-radius: 0;
		border-top-left-radius: 16px;
	}

	.btns-group button:nth-child(2) {
		background-color: #54BE7A;
		color: #FFFFFF;
		flex: 1;
		font-size: 36rpx;
		outline: none;
		border: none;
		margin: 0;
		padding: 0;
		border: 1px solid transparent;
		border-radius: 0;
	}

	.btns-group button:last-child {
		background-color: #FABD50;
		flex: 1;
		color: #FFFFFF;
		font-size: 36rpx;
		outline: none;
		border: none;
		margin: 0;
		padding: 0;
		border: 1px solid transparent;
		border-radius: 0;
		border-top-right-radius: 16px;
	}
</style>
